package aos.log;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import aos.dijkstra.GlobalVariables;

public class LogUtils {
	public static final int key = 1;
	public static final int error = 2;
	public static final int run = 4;
	public static final int info = 8;
	public static final int debug = 16;
	public static final int all = 0xffffffff;

	private static int logLevel;
	private static PrintStream out;
	private static SimpleDateFormat df;

	static {
		logLevel = all;
		out = System.out;
		df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	}

	public static void initialize(int level, String logfile) {
		logLevel = level;
		if (GlobalVariables.logfile != null
				&& !GlobalVariables.logfile.trim().equals("")) {
			try {
				out = new PrintStream(new FileOutputStream(logfile, true));
			} catch (IOException e) {
				out = System.out;
			}
		}
	}

	public static void keyLog(String log) {
		if ((logLevel & key) == 0) {
			return;
		}
		out.println(df.format(new Date()) + " KEY " + log);
	}

	public static void errorLog(String log) {
		if ((logLevel & error) == 0) {
			return;
		}
		out.println(df.format(new Date()) + " ERROR " + log);
	}

	public static void runLog(String log) {
		if ((logLevel & run) == 0) {
			return;
		}
		out.println(df.format(new Date()) + " RUN " + log);
	}

	public static void infoLog(String log) {
		if ((logLevel & info) == 0) {
			return;
		}
		out.println(df.format(new Date()) + " INFO " + log);
	}

	public static void debugLog(String log) {
		if ((logLevel & debug) == 0) {
			return;
		}
		out.println(df.format(new Date()) + " DEBUG " + log);
	}

}
